<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/Blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/Blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/Blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/Blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/Blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/Blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/Blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="vue,vuex," />










<meta name="description" content="背景什么是 vuexvuex 是一种状态管理模式，或者说我们可以称其为数据中心。 为什么使用 vuex这就相当于一个集团，集团下有很多下属公司。下属公司大了之后，有些事务可能是需要集团总部集中处理，以便提高效率，降低人员冗余。vuex 就是相当于这个中央数据处理部门。我们可以把 vuex 看做总部集团保存的各下属公司都可能用到的数据。 何时使用 vuex相对于本地组件直接调用数据而言，vuex 操">
<meta property="og:type" content="article">
<meta property="og:title" content="vuex的简单理解">
<meta property="og:url" content="https://jingping-ye.github.io/Blog/2019/01/18/vuex%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/index.html">
<meta property="og:site_name" content="jingping.ye">
<meta property="og:description" content="背景什么是 vuexvuex 是一种状态管理模式，或者说我们可以称其为数据中心。 为什么使用 vuex这就相当于一个集团，集团下有很多下属公司。下属公司大了之后，有些事务可能是需要集团总部集中处理，以便提高效率，降低人员冗余。vuex 就是相当于这个中央数据处理部门。我们可以把 vuex 看做总部集团保存的各下属公司都可能用到的数据。 何时使用 vuex相对于本地组件直接调用数据而言，vuex 操">
<meta property="og:image" content="https://i.imgur.com/1dvo9IK.png">
<meta property="og:image" content="https://i.imgur.com/gHF9WZj.gif">
<meta property="article:published_time" content="2019-01-18T07:10:12.000Z">
<meta property="article:modified_time" content="2020-05-14T05:43:21.253Z">
<meta property="article:author" content="jingping.ye">
<meta property="article:tag" content="vue">
<meta property="article:tag" content="vuex">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.imgur.com/1dvo9IK.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/Blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://jingping-ye.github.io/Blog/2019/01/18/vuex的简单理解/"/>





  <title>vuex的简单理解 | jingping.ye</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/Blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">jingping.ye</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">jingping.ye的Blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/Blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/Blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/Blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/Blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://jingping-ye.github.io/Blog/Blog/2019/01/18/vuex%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jingping.ye">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/Blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="jingping.ye">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">vuex的简单理解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-18T15:10:12+08:00">
                2019-01-18
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2020-05-14T13:43:21+08:00">
                2020-05-14
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/Blog/categories/%E6%8A%80%E6%9C%AF/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><h3 id="什么是-vuex"><a href="#什么是-vuex" class="headerlink" title="什么是 vuex"></a>什么是 vuex</h3><p>vuex 是一种状态管理模式，或者说我们可以称其为数据中心。</p>
<h3 id="为什么使用-vuex"><a href="#为什么使用-vuex" class="headerlink" title="为什么使用 vuex"></a>为什么使用 vuex</h3><p>这就相当于一个集团，集团下有很多下属公司。下属公司大了之后，有些事务可能是需要集团总部集中处理，以便提高效率，降低人员冗余。vuex 就是相当于这个中央数据处理部门。我们可以把 vuex 看做总部集团保存的各下属公司都可能用到的数据。</p>
<h3 id="何时使用-vuex"><a href="#何时使用-vuex" class="headerlink" title="何时使用 vuex"></a>何时使用 vuex</h3><p>相对于本地组件直接调用数据而言，vuex 操作更加复杂，而且也意味着额外的一些开销。</p>
<h4 id="vuex-的替代方案"><a href="#vuex-的替代方案" class="headerlink" title="vuex 的替代方案"></a>vuex 的替代方案</h4><ul>
<li>props</li>
<li>provide/inject</li>
<li>Portals/ Portal-Vue<br>参照以下文档:<br><img src="https://i.imgur.com/1dvo9IK.png" alt="何时使用vuex"></li>
</ul>
<h2 id="安装-vuex"><a href="#安装-vuex" class="headerlink" title="安装 vuex"></a>安装 vuex</h2><p><code>npm install vuex --save</code></p>
<h2 id="建立一个-vuex"><a href="#建立一个-vuex" class="headerlink" title="建立一个 vuex"></a>建立一个 vuex</h2><ol>
<li>在入口文件<code>main.js</code>中引入并使用</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import Vuex from &#39;vuex&#39;</span><br><span class="line">Vue.use(Vuex)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>按照 vuex 的模式建立一个文件<br>文件:<code>src/store/store.js</code></li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;;</span><br><span class="line">import Vuex from &#39;vuex&#39;;</span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line">const state &#x3D; &#123;&#125;</span><br><span class="line">const getters &#x3D; &#123;&#125;</span><br><span class="line">const mutations &#x3D; &#123;&#125;</span><br><span class="line">const actions &#x3D; &#123;&#125;</span><br><span class="line"></span><br><span class="line">export default new Vuex.Store(&#123;</span><br><span class="line">  state,</span><br><span class="line">  mutations,</span><br><span class="line">  actions,</span><br><span class="line">  getters</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>将 store 文件挂载在 vue 实例下</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">new Vue(&#123;</span><br><span class="line">  store,</span><br><span class="line">  el: &#39;#app&#39;,</span><br><span class="line">  router,</span><br><span class="line">  components: &#123; App &#125;,</span><br><span class="line">  template: &#39;&lt;App&#x2F;&gt;&#39;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="vuex-中的四个概念"><a href="#vuex-中的四个概念" class="headerlink" title="vuex 中的四个概念"></a>vuex 中的四个概念</h2><blockquote>
<p>vuex 中有四个概念：state、mutations、getters、actions。我们可以想象为这是总公司管理公共数据的一个部门，这个部门执行的严格管理的策略。state,代表的是这个部门管理的公共数据;mutations:对内事务管理部，修改 state，由 mutations 执行，也就是时候，要修改 state，必须经过 mutations；getters:对外输送数据部门，只输送数据，不进行其他操作;actions,中心事件,可存储中心事件，也可调用内部事件，也就是说，要进行内部事件的调用，必须由 actions 对接。</p>
</blockquote>
<h3 id="state"><a href="#state" class="headerlink" title="state"></a>state</h3><p>存储一个中心数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const state &#x3D; &#123;</span><br><span class="line">  count:0</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="mutations"><a href="#mutations" class="headerlink" title="mutations"></a>mutations</h3><p>修改一个中心数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const mutations &#x3D; &#123;</span><br><span class="line">	add()&#123;</span><br><span class="line">		++ state.count;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="getters"><a href="#getters" class="headerlink" title="getters"></a>getters</h3><p>向模板返回一个中心数据，类似于 computed<br>格式:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const getters &#x3D; &#123;</span><br><span class="line">  DataName(state) &#123;</span><br><span class="line">    return state.DataName;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>举例:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const getters &#x3D; &#123;</span><br><span class="line">  count(state) &#123;</span><br><span class="line">    return state.count;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="actions"><a href="#actions" class="headerlink" title="actions"></a>actions</h3><p>编写一个内部事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const actions &#x3D; &#123;</span><br><span class="line">  alertOne () &#123;</span><br><span class="line">    alert(&#39;1&#39;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>调用内部事件并向模板返回，通过<code>commit</code>通行证<br>格式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const actions &#x3D; &#123;</span><br><span class="line">  actionName(&#123; commit &#125;) &#123;</span><br><span class="line">    commit([mutationName]);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const actions &#x3D; &#123;</span><br><span class="line">  add (&#123; commit &#125;) &#123;</span><br><span class="line">    commit(&#39;addAge&#39;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="子部门和中心部门通信"><a href="#子部门和中心部门通信" class="headerlink" title="子部门和中心部门通信"></a>子部门和中心部门通信</h3><p>我们通过对外的部门获得 vuex 存储的中心数据和操作中心事件</p>
<ol>
<li>在 componnets 文件中导入</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import &#123; mapGetters &#125; from &#39;vuex&#39;;</span><br><span class="line">computed: mapGetters([&#39;person&#39;])</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>在 computed 中返回中心数据，在 methods 中返回中心事件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">methods: &#123;...mapActions([&#39;add&#39;])&#125;,</span><br><span class="line">computed: &#123;...mapGetters([&#39;count&#39;])&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>在模板中调用</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">计数:&#123;&#123;count&#125;&#125;</span><br><span class="line">&lt;input type&#x3D;&quot;button&quot; value&#x3D;&quot;增加&quot; @click&#x3D;&quot;add&quot;&gt;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>总结</li>
</ol>
<ul>
<li>获取中心数据:Module –&gt; Getters –&gt; State</li>
<li>操作中心数据:Module –&gt; Actions –&gt; Mutations –&gt; State</li>
</ul>
<h2 id="vuex-实例"><a href="#vuex-实例" class="headerlink" title="vuex 实例"></a>vuex 实例</h2><h3 id="计数器"><a href="#计数器" class="headerlink" title="计数器"></a>计数器</h3><ul>
<li>工具:npm/cnpm/nodejs,</li>
<li>此处为 cnpm 安装 cnpm `npm install -g cnpm –registry=<a href="https://registry.npm.taobao.org" target="_blank" rel="noopener">https://registry.npm.taobao.org</a></li>
</ul>
<ol>
<li>安装 vue-cli<br><code>cnpm install vue-cli -g</code></li>
<li>利用 webpack 生成一个 vue 项目，名称为 vuex-test<br><code>vue init webpack vuex-test</code></li>
<li>进入 vuex-test 并运行查看</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd vuex-test</span><br><span class="line">cnpm run dev</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>安装 vuex 并在入口文件 main.js 中引入</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  命令行</span><br><span class="line">npm install vuex --save</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;  main.js</span><br><span class="line">import Vuex from &#39;vuex&#39;</span><br><span class="line">Vue.use(Vuex)</span><br></pre></td></tr></table></figure>

<ol start="5">
<li>新建一个<code>vuex-test/src/store/index.js</code>文件</li>
<li>根据 store 模板写入 index.js 文件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#39;vue&#39;;</span><br><span class="line">import Vuex from &#39;vuex&#39;;</span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line">const state &#x3D; &#123;&#125;</span><br><span class="line">const getters &#x3D; &#123;&#125;</span><br><span class="line">const mutations &#x3D; &#123;&#125;</span><br><span class="line">const actions &#x3D; &#123;&#125;</span><br><span class="line"></span><br><span class="line">export default new Vuex.Store(&#123;</span><br><span class="line">  state,</span><br><span class="line">  mutations,</span><br><span class="line">  actions,</span><br><span class="line">  getters</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ol start="7">
<li>在<code>main.js</code>中引入并将其挂载在 vuex 下</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">import store from &#39;.&#x2F;store&#x2F;index&#39;</span><br><span class="line">new Vue(&#123;</span><br><span class="line">  store,</span><br><span class="line">  el: &#39;#app&#39;,</span><br><span class="line">  router,</span><br><span class="line">  components: &#123; App &#125;,</span><br><span class="line">  template: &#39;&lt;App&#x2F;&gt;&#39;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ol start="8">
<li>在<code>store.js</code>中声明一个数据<code>count</code>,并为其写一个内部操作事件和外部调用事件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  声明count</span><br><span class="line">const state &#x3D; &#123;</span><br><span class="line">  count: 0</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;  内部操作事件</span><br><span class="line">addCount (state, n) &#123;</span><br><span class="line">    let addNum &#x3D; parseInt(n)</span><br><span class="line">    state.count +&#x3D; addNum</span><br><span class="line">  &#125;</span><br><span class="line">&#x2F;&#x2F; 外部调用事件</span><br><span class="line">const actions &#x3D; &#123;</span><br><span class="line">  addCount (&#123; commit &#125;, addNum) &#123;</span><br><span class="line">    commit(&#39;addCount&#39;, addNum)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="9">
<li>新建<code>src/components/add-count.vue</code></li>
<li>在<code>add-count</code>文件中写入以下内容</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div class&#x3D;&quot;hello&quot;&gt;</span><br><span class="line">    请输入一个数:&lt;input type&#x3D;&quot;text&quot;</span><br><span class="line">      v-model&#x3D;&quot;addNum&quot;&gt;&lt;button @click&#x3D;&quot;addCount(addNum)&quot;&gt;增加&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123; mapActions &#125; from &#39;vuex&#39;;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &#39;HelloWorld&#39;,</span><br><span class="line">  data () &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      addNum: null</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    ...mapActions([&#39;addCount&#39;])</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Add &quot;scoped&quot; attribute to limit CSS to this component only --&gt;</span><br><span class="line">&lt;style scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<ol start="11">
<li>在<code>HelloWorld</code>文件中调用<code>add-count.vue</code>文件。<br>写入以下内容</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div class&#x3D;&quot;hello&quot;&gt;</span><br><span class="line">    &lt;add-count&gt;&lt;&#x2F;add-count&gt;</span><br><span class="line">    总数:&#123;&#123;count&#125;&#125;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &#123; mapGetters &#125; from &#39;vuex&#39;;</span><br><span class="line">import AddCount from &#39;.&#x2F;add-count&#39;;</span><br><span class="line">export default &#123;</span><br><span class="line">  components: &#123;</span><br><span class="line">    AddCount</span><br><span class="line">  &#125;,</span><br><span class="line">  name: &#39;HelloWorld&#39;,</span><br><span class="line">  data () &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      msg: &#39;Welcome to Your Vue.js App&#39;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123; ...mapGetters([&#39;count&#39;]) &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Add &quot;scoped&quot; attribute to limit CSS to this component only --&gt;</span><br><span class="line">&lt;style scoped&gt;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<ol start="12">
<li>效果</li>
</ol>
<p><img src="https://i.imgur.com/gHF9WZj.gif" alt="addCount"></p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>[1]<a href="https://vuex.vuejs.org/zh/" target="_blank" rel="noopener">vuex 是什么？ | vuex</a></p>
<p>[2]<a href="https://markus.oberlehner.net/blog/should-i-store-this-data-in-vuex/" target="_blank" rel="noopener">should i store this data in vuex? - markus oberlehner</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/Blog/tags/vue/" rel="tag"># vue</a>
          
            <a href="/Blog/tags/vuex/" rel="tag"># vuex</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/Blog/2019/01/15/CSS%E4%B8%8D%E5%90%8C%E7%B1%BB%E5%9E%8B%E4%B8%8E%E4%BD%BF%E7%94%A8%E4%B8%8D%E5%90%8C%E7%B1%BB%E5%9E%8B%E7%9A%84%E4%BC%98%E7%BC%BA%E7%82%B9/" rel="next" title="CSS不同类型与使用不同类型的优缺点">
                <i class="fa fa-chevron-left"></i> CSS不同类型与使用不同类型的优缺点
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/Blog/2019/04/03/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%9A%84%E6%80%9D%E8%B7%AF%E4%B8%8E%E6%96%B9%E6%B3%95/" rel="prev" title="快速排序的思路与方法">
                快速排序的思路与方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">jingping.ye</p>
              <p class="site-description motion-element" itemprop="description">jingping.ye的Blog|文章</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/Blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/Blog/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/Blog/tags/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yourname" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:yourname@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/yourname" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yourname" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.facebook.com/yourname" target="_blank" title="FB Page">
                      
                        <i class="fa fa-fw fa-facebook"></i>FB Page</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://vk.com/yourname" target="_blank" title="VK Group">
                      
                        <i class="fa fa-fw fa-vk"></i>VK Group</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://stackoverflow.com/yourname" target="_blank" title="StackOverflow">
                      
                        <i class="fa fa-fw fa-stack-overflow"></i>StackOverflow</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://youtube.com/yourname" target="_blank" title="YouTube">
                      
                        <i class="fa fa-fw fa-youtube"></i>YouTube</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://instagram.com/yourname" target="_blank" title="Instagram">
                      
                        <i class="fa fa-fw fa-instagram"></i>Instagram</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="skype:yourname?call|chat" target="_blank" title="Skype">
                      
                        <i class="fa fa-fw fa-skype"></i>Skype</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#背景"><span class="nav-number">1.</span> <span class="nav-text">背景</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#什么是-vuex"><span class="nav-number">1.1.</span> <span class="nav-text">什么是 vuex</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么使用-vuex"><span class="nav-number">1.2.</span> <span class="nav-text">为什么使用 vuex</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#何时使用-vuex"><span class="nav-number">1.3.</span> <span class="nav-text">何时使用 vuex</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#vuex-的替代方案"><span class="nav-number">1.3.1.</span> <span class="nav-text">vuex 的替代方案</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装-vuex"><span class="nav-number">2.</span> <span class="nav-text">安装 vuex</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#建立一个-vuex"><span class="nav-number">3.</span> <span class="nav-text">建立一个 vuex</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vuex-中的四个概念"><span class="nav-number">4.</span> <span class="nav-text">vuex 中的四个概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#state"><span class="nav-number">4.1.</span> <span class="nav-text">state</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mutations"><span class="nav-number">4.2.</span> <span class="nav-text">mutations</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#getters"><span class="nav-number">4.3.</span> <span class="nav-text">getters</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#actions"><span class="nav-number">4.4.</span> <span class="nav-text">actions</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#子部门和中心部门通信"><span class="nav-number">4.5.</span> <span class="nav-text">子部门和中心部门通信</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vuex-实例"><span class="nav-number">5.</span> <span class="nav-text">vuex 实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#计数器"><span class="nav-number">5.1.</span> <span class="nav-text">计数器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考"><span class="nav-number">6.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jingping.ye</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">15k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/Blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/Blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/Blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/Blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/Blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/Blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
